<template>
  <div class="foodcontent">
    <top-header :isLeft="true" :title="foodname"></top-header>
    <div>
      <div v-for="(item, index) in foodstore" :key="index" class="share">
        <div v-show="item.content.object.name == foodname" class="ifm">
          <img :src="getImage(item.content.object.photo)" alt="" />
          <div class="title">
            <p class="name">{{ item.content.object.name }}</p>
            <p v-if="item.content.object.score != ''">
              {{ item.content.object.score }}<span>综合评分</span>
            </p>
          </div>
          <hr />

          <div class="user">
            <img
              :src="getImage(url + item.content.object.author.image.ident)"
              alt=""
              @click="toauthor(item)"
            />
            <p class="username" @click="toauthor(item)">
              {{ item.content.object.author.name }}
            </p>
            <button class="button2" @click="yiguanzhu()" v-if="show == 1">
              已关注
            </button>
            <button @click="guanzhu()" v-else>关注</button>
            <p>{{ item.content.object.desc }}</p>
          </div>

          <div class="cailiao">
            <p>用料</p>
            <button @click="outcailanzi(item)" v-if="showcai == 'a'">从菜篮子中移除</button>
            <button @click="tocailanzi(item)" v-else>丢进菜篮子里</button>
            
            <div class="list">
              <div
                v-for="(i, index) in item.content.object.ingredient"
                :key="index - 1"
              >
                <p class="cailiaoname">{{ i.name }}</p>
              </div>
            </div>
          </div>
          <div class="author_food">
            <p>作者菜谱</p>
          </div>
        </div>
        <div class="author_all">
          <div
            v-if="item.content.object.author.name == foodauthor"
            class="author_alllist"
          >
            <div>
              <img :src="getImage(item.content.object.photo)" alt="" />
              <div class="author_message">
                <p>{{ item.content.object.name }}</p>
                <div>
                  <img
                    :src="
                      getImage(url + item.content.object.author.image.ident)
                    "
                    alt=""
                  />
                </div>
              </div>
              <p class="username1">{{ item.content.object.author.name }}</p>
            </div>
          </div>
        </div>
      </div>
      <div>
        <!-- <div v-show="show == 'A'">
          <p>已加入我的收藏</p>
        </div> -->
        <table>
          <tr>
            <td v-if="show1 == 'A'">
              <p class="yishoucang"><i class="iconfont">&#xe60f;</i>已收藏</p>
            </td>
            <td @click="shoucang()" active-class="bianse" v-else>
              <p><i class="iconfont">&#xe60f;</i>收藏</p>
            </td>

            <td @click="towork()">
              <p><i class="iconfont">&#xe90b;</i>分享</p>
            </td>
          </tr>
        </table>
      </div>
    </div>
    <img src="../assets/imgs/service/线上菜篮子.png" alt="" class="cailanzi" v-show="showcailanzi == 1" @click="$router.push({name:'VegetableBasket'})">
    <div class="null"></div>
  </div>
</template>

<script>
import TopHeader from "../components/TopHeader.vue";
import QS, { parse } from "qs";
export default {
  components: { TopHeader },
  data() {
    return {
      foodname: "",
      foodstore: "",
      foodauthor: "",
      foodicon: "",
      foodimg: "",
      foodscore: "",
      url: "https://i2.chuimg.com/",
      num: 0,
      show: "",
      icon: "",
      name: "",
      show1: "",
      shicai:'',
      showcai:'',
      showcailanzi:''
    };
  },
  mounted() {
    this.foodname = this.$store.getters.food.name;
    // console.log(this.foodname)
      this.foodstore = this.$store.getters.foods;
      this.foodauthor = this.$store.getters.food.author;
      // console.log(this.foodauthor)
      this.foodicon = this.$store.getters.food.icon;
      this.foodimg = this.$store.getters.food.img;
      this.foodscore = this.$store.getters.food.score;
  },
  created() {
    this.getparams();
    this.shows();
  },
  methods: {
    guanzhu() {
      //   this.num += 1
      //   this.$store.dispatch('getNum',this.num)
      this.show = 1;

      let params = {
        name: this.foodauthor,
        icon: this.foodicon,
        type: 1,
        user_id:localStorage.id

      };
      this.$ajax.post("/mysql/author.php", QS.stringify(params)).then((res) => {
        console.log(res);
      });
    },
    shows() {
      this.$ajax.get("/mysql/authorselect.php",{
        params:{
          id:localStorage.id
        }
      }).then((res) => {
        console.log(res);
        let arr = res.data;
        //    console.log(this.name)
        var result = arr.some((item) => {
          //    console.log(item)
          if (item.name == this.foodauthor) {
            return true;
          }
        });
        console.log(result);
        if (result) {
          this.show = 1;
        }
      });
      this.$ajax.get("/mysql/shoucangselect.php",{
        params:{
          id:localStorage.id
        }
      }).then((res) => {
        console.log(res);
        let arr = res.data;
        //    console.log(this.name)
        var result = arr.some((item) => {
          //    console.log(item)
          if (item.title == this.foodname) {
            return true;
          }
        });
        console.log(result);
        if (result) {
          this.show1 = "A";
        }
      });
        this.$ajax.get("/mysql/shicaiselect.php",{
          params:{
            id:localStorage.id
          }
        }).then((res) => {
        console.log(res);
        let arr = res.data;
        //    console.log(this.name)
        var result = arr.some((item) => {
          //    console.log(item)
          if (item.foodname == this.foodname) {
            return true;
          }
        });
        console.log(result);
        if (result) {
          this.showcai = "a";
        }
      });
      this.$ajax.get('/mysql/shicaiselect.php',{
        params:{
          id:localStorage.id
        }
      }).then((res)=>{
        // console.log(res)
        if(res.data.length != 0){
          this.showcailanzi = 1
        }
      })
    },
    yiguanzhu() {
      //   this.num -= 1;
      this.show = 0;
      console.log(this.num);
      this.$ajax
        .get("/mysql/authordel.php", {
          params: {
            name: this.foodauthor,
          },
        })
        .then((res) => {
          console.log(res);
        });
    },
    getparams() {
      // this.foodname = this.$route.query.name;
      // this.foodstore = this.$route.query.store;
      // this.foodauthor = this.$route.query.author;
      // this.foodicon = this.$route.query.icon;
      // this.foodimg = this.$route.query.img;
      // this.foodscore = this.$route.query.score;
      // console.log(this.foodname);
      // console.log(this.foodstore);
      // console.log(this.foodicon);
    },
    getImage(url) {
      // console.log(url);
      if (url !== undefined) {
        return url.replace(
          /^(http)[s]*(\:\/\/)/,
          "https://images.weserv.nl/?url="
        );
      }
    },
    shoucang() {
      this.show1 = "A";
      let params = {
        author: this.foodauthor,
        title: this.foodname,
        img: this.foodimg,
        score: this.foodscore,
        user_id:localStorage.id
        // type:1
        // foodsname:this.foodname
      };
      this.$ajax
        .post("/mysql/shoucang.php", QS.stringify(params))
        .then((res) => {
          console.log(res);
        });
    },
    toauthor(item) {
      this.$router.push({
        name: "UserDetails",
        query: {
          store: this.foodstore,
          // name: this.name,
          author: this.foodauthor,
          icon: this.foodicon,
          // img:this.img,
          // score:this.score
        },
      });
    },
    tocailanzi(item){
      this.showcai = 'a'
      this.shicai = JSON.stringify(item.content.object.ingredient)
      // console.log(this.shicai)
  
          let params = {
      food:this.shicai,
      foodname:this.foodname,
      user_id:localStorage.id
      };
      this.$ajax.post("/mysql/shicai.php", QS.stringify(params)).then((res) => {
        console.log(res);
      }); 
    },
    outcailanzi(item){
      this.showcai = 'b'
      this.$ajax
        .get("/mysql/shicaidel.php", {
          params: {
            foodname: this.foodname,
          },
        })
        .then((res) => {
          console.log(res);
        });
    },
    towork(){
      // this.foodimg = JSON.stringify(this.foodimg)
      let params = {
        img:this.foodimg,
        content:this.foodname,
        user_id:localStorage.id
      };
      this.$ajax.post('/mysql/share.php',QS.stringify(params)).then((res)=>{
        console.log(res)
        this.$router.push({name:'Home'})
      })
    }
  },
};
</script>
<style lang="scss" scoped>
.ifm {
  margin-top: 60px;
  width: 375.2px;
  img {
    width: 100%;
    height: 350px;
  }
  .title {
    color: black;
    font-size: 18px;
    text-align: center;
    .name {
      margin: 10px 0;
      font-size: 30px;
      font-weight: bold;
    }
  }
  .user {
    margin: 0 10px;

    img {
      display: inline-block;
      width: 90px;
      height: 90px;
      border-radius: 50%;
    }
    .username {
      display: inline-block;
      margin-left: 10px;

      font-size: 20px;
    }
    button {
      border: 1px solid rgb(241, 167, 179);
      color: rgb(241, 148, 164);
      float: right;
      margin-top: 28px;
      width: 60px;
      height: 35px;
      background: white;
    }
    .button2 {
      border: 1px solid gray;
      color: gray;
      float: right;
      margin-top: 28px;
      width: 60px;
      height: 35px;
      background: white;
    }
    p {
      font-size: 20px;
      margin-top: 15px;
    }
  }
}
table {
  background: white;
  position: fixed;
  bottom: 0;
  margin-top: 20px;
  width: 100%;
  height: 50px;
  td {
    font-size: 20px;
    width: 50%;
    text-align: center;
    i {
      font-size: 20px;
      margin-right: 5px;
    }
    p {
      margin: 0;
    }
    .yishoucang {
      color: red;
    }
  }
}
.cailiao {
  margin: 0 10px;
  p {
    font-size: 20px;
    font-weight: bolder;
    color: black;
  }
  button {
    float: right;
    margin-top: -50px;
    padding: 5px 10px;
    border-radius: 20px;
    background-color: white;
    border: 1px solid rgb(179, 173, 173);
  }
  .list {
    display: flex;
    justify-content: space-between;
    flex-direction: row;
    flex-wrap: wrap;
    .cailiaoname {
      color: rgb(116, 113, 113);
      width: fit-content;
    }
  }
}
.author_food {
  margin: 0 10px;
  color: black;
  font-size: 20px;
  font-weight: bold;
}
.author_all {
  width: 340px;
  margin-left: 10px;
  .author_alllist {
    display: flex;
    width: 100%;

    img {
      width: 350px;
      height: 280px;
    }
    .author_message {
      margin-top: 10px;
      display: flex;
      justify-content: space-between;
      p {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        width: 280px;
        font-size: 25px;
      }
      img {
        margin-right: 0;
        width: 50px;
        height: 50px;
        border-radius: 50%;
      }
    }
  }
}
.username1 {
  font-size: 15px;
  float: right;
}
.bianse {
  color: red;
}
.cailanzi{
  position: fixed;
  left: 10px;
  bottom: 80px;
  width: 50px;
  height: 50px;
}
.null {
  height: 40px;
}
</style>